package com.frogs.ucenter.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApplicablePlatformEnum;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.ucenter.component.UserPermissionHelper;
import com.frogs.ucenter.entity.UserBaseAccountInfo;
import com.frogs.ucenter.service.ShoppingCartService;

/**
 * 验证检查类
 * @author wushubin
 *
 */
@Controller
public class ShoppingCartController extends BaseController3 {

	private static Logger log = LoggerFactory.getLogger(ShoppingCartController.class);

	private static final String HOME_VIEW = "shoppingCart/index";
	
	private static final String DELETE = "/shoppingCart/delete";
	private static final String UPDATE = "/shoppingCart/update";
	private static final String SUM = "/shoppingCart/sum";
	private static final String HOME = "/shoppingCart/index";
	private static final String PARTIAL_LIST = "/shoppingCart/partialList";
	
	@Resource
	private ShoppingCartService shoppingService;
	
	@Resource
	private UserPermissionHelper userPermissionHelper;
	
	@RequestMapping(DELETE)
    public void delete(HttpServletRequest request, ModelMap model, Integer id, Integer applicablePlatformId, Integer skNum) throws Exception {
    	try {
    		
    		
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		model.addAttribute("result", shoppingService.deleteShoppingCart(user.getId(), id,applicablePlatformId));
    		shoppingService.rockBackSeckillRestriction(user.getId(), id, skNum);
		} catch (Exception e) {
			log.error("func[delete] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
    }
	
	@RequestMapping(UPDATE)
    public void update(HttpServletRequest request ,ModelMap model,Integer id,Integer amount) throws Exception {
    	try {
    		
    		
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		model.addAttribute("result", shoppingService.updateShoppingCart(user.getId(), id, amount));
		} catch (Exception e) {
			log.error("func[update] parameterMap[{}] exception[{} - {}] desc[fail]",
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
    }
	
	@RequestMapping(SUM)
	public JSONObject sum(HttpServletRequest request ,ModelMap model) throws Exception {
		try {
			
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			
			JSONObject result = shoppingService.sumShoppingCart(user.getId());
			if(null != result && 
					ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue("code")) {
				return result;
			}
		} catch (Exception e) {
			log.error("func[sum] parameterMap[{}] exception[{} - {}] desc[fail]",
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		
		return null;
	}
	
	@RequestMapping(HOME)
    public String home(HttpServletRequest request, ModelMap model) {
    	try {
    		HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    		
    		
    		Integer pageNo = reqInfo.getIntParameter("pageNo", 0);
    		Integer pageSize = reqInfo.getIntParameter("pageSize", 20);
    		Integer applicablePlatformId = reqInfo.getIntParameter("applicablePlatformId", ApplicablePlatformEnum.WEB.getId());
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		List<Map<String,Object>> list = shoppingService.getShoppingCartList(user.getId(), applicablePlatformId , pageNo,pageSize);
    		model.addAttribute("list",list );
    		
		} catch (Exception e) {
			log.error("func[home] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
    	return HOME_VIEW;
    }
	
	@RequestMapping(PARTIAL_LIST)
	public JSONObject listShoppingCart(HttpServletRequest request) {
		try {
    		HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    		
    		JSONObject resultJson = new JSONObject();
    		Integer pageNo = reqInfo.getIntParameter("pageNo", 1);
    		Integer pageSize = reqInfo.getIntParameter("pageSize", 3);
    		Integer applicablePlatformId = reqInfo.getIntParameter("applicablePlatformId", ApplicablePlatformEnum.WEB.getId());
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		List<Map<String,Object>> list = shoppingService.getShoppingCartList(user.getId(), applicablePlatformId , pageNo,pageSize);
    		resultJson.put("list", JSONArray.toJSON(list));
    		return resultJson;
		} catch (Exception e) {
			log.error("func[listShoppingCart] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			request.setAttribute("errorMessage", e.getMessage());
			return null;
		}
	}
}
